﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EngineOfEvermore.Scene;
using NUnit.Framework;
using EngineOfEvermore.Editor.Common.Command;
using EngineOfEvermore.Editor.SceneEditor;
using NUnit.Mocks;
using EngineOfEvermore.Common;
using System.Diagnostics;

namespace EngineOfEvermore.Editor.SceneTools
{
    interface ISelectionCommandAdapter
    {
    	ISceneSelection Selection { get; }
        ICommandStack CommandStack { get; }
        
        void AddToSelection( IEnumerable<SceneObject> items );
        void RemoveFromSelection( IEnumerable<SceneObject> items );
        void SetSelection( IEnumerable<SceneObject> items );
        void ClearSelection();
    }

    class SelectionCommandAdapter : ISelectionCommandAdapter
    {
        public ISceneSelection Selection { get; private set; }
        public ICommandStack CommandStack { get; private set; }

        public SelectionCommandAdapter( ISceneSelection selection, ICommandStack commandStack )
        {
            Debug.Assert( selection != null );
            Debug.Assert( commandStack != null );

            this.Selection = selection;
            this.CommandStack = commandStack;
        }

        public void AddToSelection( IEnumerable<SceneObject> items )
        {
            var command = new AddToSelectionCommand( this.Selection, items );
            this.CommandStack.PushCommand( command );
            command.Execute();
        }

        public void RemoveFromSelection( IEnumerable<SceneObject> items )
        {
            var command = new RemoveFromSelectionCommand( this.Selection, items );
            this.CommandStack.PushCommand( command );
            command.Execute();
        }

        public void SetSelection( IEnumerable<SceneObject> items )
        {
            var command = new SetSelectionCommand( this.Selection, items );
            this.CommandStack.PushCommand( command );
            command.Execute();
        }

        public void ClearSelection()
        {
            var command = new ClearSelectionCommand( this.Selection );
            this.CommandStack.PushCommand( command );
            command.Execute();
        }

        #if ENGINE_OF_EVERMORE_ENABLE_UNIT_TESTS
        [TestFixture]
        public class Test
        {
            ISceneSelection _sceneSelection;
            MockCommandStack _commandStack;

            private SelectionCommandAdapter _createMockedCommandAdapter()
            {
                return new SelectionCommandAdapter( _sceneSelection, _commandStack );
            }

            [SetUp]
            public void SetUp()
            {
                _sceneSelection = new SceneSelection();
                _commandStack = new MockCommandStack();
            }

            [Test]
            public void TestAddToSelection()
            {
                SceneObject[] items = Util.Generate( () => new SceneObject(), 2 ).ToArray();

                
                var adapter = _createMockedCommandAdapter();

                adapter.AddToSelection( items );
                
                Assert.That( _commandStack.Commands, Has.Count.EqualTo( 1 ) );
                Assert.That( _commandStack.Commands.Peek() is AddToSelectionCommand );
                Assert.That( _sceneSelection, Is.EquivalentTo( items ) );
            }

            [Test]
            public void TestRemoveFromSelection()
            {
                var allItems = Util.Generate( () => new SceneObject(), 4 ).ToArray();
                var itemsToBeRemoved = allItems.Skip( 2 );
                var itemsRemaining = allItems.Except( itemsToBeRemoved ).ToArray();

                _sceneSelection.Set( allItems );

                var adapter = _createMockedCommandAdapter();

                adapter.RemoveFromSelection( itemsToBeRemoved );

                Assert.That( _commandStack.Commands, Has.Count.EqualTo( 1 ) );
                Assert.That( _commandStack.Commands.Peek() is RemoveFromSelectionCommand );
                Assert.That( _sceneSelection, Is.EquivalentTo( itemsRemaining ) );
            }

            [Test]
            public void TestSetSelection()
            {
                var oldItems = Util.Generate( () => new SceneObject(), 4 ).ToArray();
                var newItems = Util.Generate( () => new SceneObject(), 4 ).ToArray();

                _sceneSelection.Set( oldItems );

                var adapter = _createMockedCommandAdapter();

                adapter.SetSelection( newItems );

                Assert.That( _commandStack.Commands, Has.Count.EqualTo( 1 ) );
                Assert.That( _commandStack.Commands.Peek() is SetSelectionCommand );
                Assert.That( _sceneSelection, Is.EquivalentTo( newItems ) );
            }

            [Test]
            public void TestClearSelection()
            {
                var oldItems = Util.Generate( () => new SceneObject(), 4 ).ToArray();
                
                _sceneSelection.Set( oldItems );

                var adapter = _createMockedCommandAdapter();

                adapter.ClearSelection();

                Assert.That( _commandStack.Commands, Has.Count.EqualTo( 1 ) );
                Assert.That( _commandStack.Commands.Peek() is ClearSelectionCommand );
                Assert.That( _sceneSelection, Is.Empty );
            }
        }
        #endif
    }
}
